app.controller('userMonitorCtrl', function ($scope, userMonitorService, $timeout) {
    var flag;
    var getAll = function () {
        userMonitorService.getUserMonitor({
            action: 'getAllMonitor'
        }, function (data) {
            if (data.state == 'SUCCESS') {
                $scope.userMonitors = data.data.list;
            }
        });
        $timeout(function () {
            getAll();
        }, $scope.selectTime);
    };
    $scope.changeTime = function (time) {
        $scope.sysTime = time;
    }
    var getAllService = function () {
        userMonitorService.getService({
            action: 'getAllService'
        }, function (data) {
            if (data.state == 'SUCCESS') {
                // AMQS
                $scope.amqsPort = data.data.list[0].port;
                $scope.amqsHost = data.data.list[0].host;
                $scope.amqsChannel = data.data.list[0].channel;
                $scope.amqsState = data.data.list[0].state;
                $scope.amqsQm = data.data.list[0].qm;
                $scope.amqsHealthy=data.data.list[0].healthy;
                $scope.amqTitle = data.data.list[0].state == 'CONNECTED' ? '正常' : '异常';
                //// MQInfo
                $scope.mqInfoPort = data.data.list[1].port;
                $scope.mqInfoHost = data.data.list[1].host;
                $scope.mqInfoName = data.data.list[1].name;
                $scope.mqInfoState = data.data.list[1].state;
                $scope.mqInfoConnectCount = data.data.list[1].connectCount;
                $scope.mqInfoStartTime = data.data.list[1].startTime;
                $scope.mqInfoTitle = data.data.list[1].state == 'Running' ? '正常' : '异常';
                // GW
                $scope.gwName =data.data.list[2].name == null ? "———  ———" : data.data.list[2].name;
                $scope.gwIp = data.data.list[2].ip;
                $scope.gwPort = data.data.list[2].port;
                $scope.gwHost = data.data.list[2].host;
                $scope.gwChannel = data.data.list[2].channel;
                $scope.gwState = data.data.list[2].state;
                $scope.gwHealthy = data.data.list[2].healthy;
                $scope.gwMode = data.data.list[2].mode == null ? "EXCEPTION" : data.data.list[2].mode;
                $scope.gwQm = data.data.list[2].qm;
                $scope.gwTitle = data.data.list[2].healthy ? '正常' : '异常';
            }
        });
        $timeout(function () {
            getAllService();
        }, $scope.sysTime);
    };

    var getMonitorLogs = function () {
        if ($scope.logCount == null) {
            $scope.logCount = '50';
        }
        userMonitorService.getMonitorLogs({
            action: 'getMonitorLogs',
            logCount: $scope.logCount
        }, function (data) {
            //展示路由数据表格
            if (data.state == 'SUCCESS') {
                $scope.logs = data.data.list;
            }
            $timeout(function () {
                getMonitorLogs();
            }, $scope.logTime);
        });
    };
    $scope.changeLogTime = function (time) {
        $scope.logTime = time;
    };
    $scope.changeLogCount = function (count) {
        $scope.logCount = count;
        getMonitorLogs();
    };

    getMonitorLogs();
    getAll();
    getAllService();
    /**
     *
     * @param 显示Dialog
     */
    $scope.showDialogStatusIp = function (ip) {
        $('#dialogStatusIp').on('show.bs.modal', function () {
            $scope.userMonitorIp = ip;
        });
        $("#stateList").text(" ");
        $("#dialogStatusIp").modal('show');
    };
    $scope.checkNet = function (cmd,ip,pingNum) {
        //初始化cmd
        if (cmd == undefined) {
            cmd = "ping";
        }
        flag = true;
        $("#stateList").text(" ");
        userMonitorService.getMonitorLogs({
                action: 'checkNet',
                cmd: cmd,
                ip: ip,
                pingNum:pingNum
            }, function (data) {
                //展示路由数据表格
              if(flag){
                if (data.state == 'SUCCESS') {
                    $("#stateList").text(data.data.message[0])
                } else {
                    $("#stateList").text("服务器响应失败或超时")
                }
              }
            }
        )
    };
    /**
     * 当模态框隐藏时触发
     */
    $('#dialogStatusIp').on('hidden.bs.modal', function () {
        flag = false;
    });
    /**
     * 当单选按钮改变是触发
     */
    $("input[type = radio]").change(function(){
        //清空上一次记录
        $("#stateList").text(" ");
        flag = false;
    })
    $scope.setBgColor = function (logLevel) {
        if (logLevel == '0') {
            return {
                'background-color': '#7AD57A'
            }
        } else if (logLevel == '1') {
            return {
                'background-color': '#FFFF00'
            }
        } else if (logLevel == '2') {
            return {
                'background-color': '#EE9A00'
            }
        } else {
            return {
                'background-color': '#D9524E'
            }
        }
    };
    /**
     *
     * 改变用户监控的高度
     */
    $scope.changeUserMonitorHeight = function () {
        //加边框宽度
        if($("#userMonitor").height()+2 == 368){
            $("#userMonitor").css("height",550);
        }else{
            $("#userMonitor").css("height",368);
        }
    };
});
